package abc.red.controller;

import abc.red.common.BaseContext;
import abc.red.entity.D;
import abc.red.entity.Manager;
import abc.red.entity.R;
import abc.red.service.ManagerService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Slf4j
@RestController
@RequestMapping("/manager")
public class ManagerController {


    @Autowired
    private ManagerService service;

    //设置分页查询的属性
    private int pageSize = 10;


    @PostMapping("/getOneById")
    public R<Manager> getOneById() {

        long id = BaseContext.getCurrentId();
        Manager sqlM = service.getById(id);

        //数据肯定存在，直接返回信息
        return R.success(sqlM);
    }

    @PostMapping("/getOneById1")
    public R<Manager> getOneById1(@RequestBody D d) {

        Manager sqlM = service.getById(d.getId());

        //数据肯定存在，直接返回信息
        return R.success(sqlM);
    }


    @PostMapping("/getAllByPage")
    public R<IPage> getAllByPage(@RequestBody D d) {
        long id = BaseContext.getCurrentId();

//        [-max,avalibel]
        IPage<Manager> page = new Page<>(d.getNum1(), pageSize);
        service.page(page);
//        System.out.println(page.getTotal());
//        System.out.println(page.getRecords());
//        for (Manager m : page.getRecords()) {
//            System.out.println(m.toString());
//        }
//        总页数
//        System.out.println(page.getPages());
//        System.out.println(page.getCurrent());
//        System.out.println(page.getSize());
        return R.success(page);

    }

    @PostMapping("/add")
    public R<String> add(@RequestBody Manager m) {
        //密码加密
        String md5Password = DigestUtils.md5DigestAsHex(m.getPassword().getBytes());
        m.setPassword(md5Password);
        boolean end = service.save(m);
        if (end) {
            log.info("打印ManagerController-add方法上传的Manager对象数据   {}",m.toString());
            return R.success("新增成功");
        } else {
            return R.error("新增失败");
        }
    }


    @PostMapping("/changeStatus")
    public R<String> changeStatus(@RequestBody D d) {
        log.info("打印参数D "+d.toString());
        Manager sqlM = service.getById(d.getId());
        sqlM.setStatus((sqlM.getStatus()+1)%2);
        //防手贱代码，保护超级管理员账号
        if(sqlM.getId()==1){
            sqlM.setStatus(1);
        }
        boolean end = service.updateById(sqlM);
        log.info("打印ManagerController-changeStatus方法上传的Manager对象数据   {}",sqlM.toString());
        if (end) {
            return R.success("修改成功");
        } else {
            return R.error("修改失败");
        }
    }


    @PostMapping("/deleteById")
    public R<String> deleteById(@RequestBody Manager m) {
        //密码加密
        if(m.getId()==1){
            return R.error("删除失败,超级管理员不可以删除");
        }
        boolean end = service.removeById(m);
        if (end) {
            log.info("打印ManagerController-deleteById方法上传的Manager对象数据   {}",m.toString());
            return R.success("删除成功");
        } else {
            return R.error("删除失败");
        }
    }


    @PostMapping("/editById")
    public R<String> editById(@RequestBody Manager m) {
        //密码加密
        String md5Password = DigestUtils.md5DigestAsHex(m.getPassword().getBytes());
        m.setPassword(md5Password);
        boolean end = service.updateById(m);
        if (end) {
            log.info("打印ManagerController-editById方法上传的Manager对象数据   {}",m.toString());
            return R.success("编辑成功");
        } else {
            return R.error("编辑失败");
        }
    }






    @PostMapping("/getAllManagersData")
    public List<Manager> getAllManagersData(){
        return service.list();

    }

}
